package com.example.thirdhomework

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.example.thirdhomework.databinding.ActivityMainBinding

const val KEY1 = "KEY1"
const val KEY2 = "KEY2"
class MainActivity : AppCompatActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(binding.root)

        binding.button.setOnClickListener {
            val intent = Intent(this,FirstActivity::class.java)
            intent.putExtra(KEY1, binding.editTextText.text.toString())
            startActivity(intent)
        }

        val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result ->
            if(result.resultCode == RESULT_OK){
                binding.textView.text = result.data?.getStringExtra(KEY2)
            }
        }

        binding.button2.setOnClickListener {
            val intent = Intent(this,SecondActivity::class.java)
            launcher.launch(intent)
        }

        binding.button3.setOnClickListener {
            val intent = Intent(this,ThirdActivity::class.java)
            startActivity(intent)
        }

        binding.button4.setOnClickListener {
            val intent = Intent(this,ForthActivity::class.java)
            startActivity(intent)
        }

    }

}

